package com.ymatou.iapi.couponmanage.testcase;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponmanage.parameter.ChangetimeinactivityBean;
import com.ymatou.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.ymatou.iapi.couponmanage.service.ChangetimeinactivityCall;
import com.ymatou.iapi.couponmanage.service.SellerCreateBatchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceMCallServiceV2;
import com.ymttest.database.model.couponbatch;
import com.ymttest.database.model.coupontask;
import com.ymttest.database.sqlwapper.couponbatchWapper;
import com.ymttest.database.sqlwapper.coupontaskWapper;
import com.ymttest.utils.TestDataManager;
/**
*改变优惠券批次时间
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_changetimeinactivity{
	private static ChangetimeinactivityBean bean;
	private static ChangetimeinactivityCall call;
	private  SellerCreateBatchBean sellercreatebatchBean=new SellerCreateBatchBean();
	private  SellerCreateBatchCall sellercreatebatchCall=new SellerCreateBatchCall();
	private couponbatch beforBatch=new  couponbatch();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("改变优惠券批次时间-1:这是一个开放式数据接口，需要客户谨慎调用，调用错误会导致时间与业务约束不一致;2:接口仅接收数据，程序异步执行");
	}

	@Before
	public void caseUp() {
		bean=new ChangetimeinactivityBean();
		call=new ChangetimeinactivityCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_changetimeinactivity_001() {
		Logger.start(true,"正例-改变优惠券批次时间，验证更新使用、过期 的开始结束时间，返回成功");
		try {
			Logger.comment("#######前置条件开始#########");
			sellercreatebatchBean = new CouponServiceMCallServiceV2().setDefaultSellerCreateBatch_ActivityBean();
			sellercreatebatchCall=new CouponServiceMCallServiceV2().SellerCreateBatch(sellercreatebatchBean);
			Logger.comment("#######前置条件结束#########");
			
			Date dNow = new Date(); // 当前时间
			Date dAcqBefore = new Date();
			Date dAcqAfter = new Date();
			Date dValidStar = new Date();
			Date dValidEnd = new Date();
			
			Calendar calendar1 = Calendar.getInstance(); 
			calendar1.setTime(dNow);
			calendar1.add(Calendar.DAY_OF_MONTH, 1); 
			dAcqBefore = calendar1.getTime(); 

			Calendar calendar2 = Calendar.getInstance(); 
			calendar2.setTime(dNow);
			calendar2.add(Calendar.DAY_OF_MONTH, 2); 
			dAcqAfter = calendar2.getTime(); 
			
			Calendar calendar3 = Calendar.getInstance(); 
			calendar3.setTime(dNow);
			calendar3.add(Calendar.DAY_OF_MONTH, 3); 
			dValidStar = calendar3.getTime(); 
			
			Calendar calendar4 = Calendar.getInstance(); 
			calendar4.setTime(dNow);
			calendar4.add(Calendar.DAY_OF_MONTH, 4); 
			dValidEnd = calendar4.getTime(); 
			
            bean.setAcquireStartTime(dAcqBefore);
            bean.setAcquireEndTime(dAcqAfter);
			bean.setEffectiveStartTime(dValidStar);
			bean.setEffectiveEndTime(dValidEnd);
			bean.setPlatformActivityId(sellercreatebatchBean.getCouponTypeBizValue());
			bean.setOperator(Integer.toString(new TestDataManager().getRandomIntNum(5)));
			call.setData(bean);
			call.callService();	
			//验证数据库
			VerifyReturnDataBase();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_changetimeinactivity_002() {
		Logger.start(true,"正例-改变优惠券批次时间，验证更新使用的开始结束时间，返回成功");
		try {
			Logger.comment("#######前置条件开始#########");
			sellercreatebatchBean = new CouponServiceMCallServiceV2().setDefaultSellerCreateBatch_ActivityBean();
			sellercreatebatchCall=new CouponServiceMCallServiceV2().SellerCreateBatch(sellercreatebatchBean);
			Logger.comment("#######前置条件结束#########");
			
		   beforBatch= new couponbatchWapper().selectBybatchCode(sellercreatebatchCall.getBatchCode()).get(0);
			
			Date dNow = new Date(); // 当前时间
			Date dAcqBefore = new Date();
			Date dAcqAfter = new Date();
//			Date dValidStar = new Date();
//			Date dValidEnd = new Date();
			
			Calendar calendar1 = Calendar.getInstance(); 
			calendar1.setTime(dNow);
			calendar1.add(Calendar.DAY_OF_MONTH, 1); 
			dAcqBefore = calendar1.getTime(); 

			Calendar calendar2 = Calendar.getInstance(); 
			calendar2.setTime(dNow);
			calendar2.add(Calendar.DAY_OF_MONTH, 2); 
			dAcqAfter = calendar2.getTime(); 
			
//			Calendar calendar3 = Calendar.getInstance(); 
//			calendar3.setTime(dNow);
//			calendar3.add(Calendar.DAY_OF_MONTH, 3); 
//			dValidStar = calendar3.getTime(); 
//			
//			Calendar calendar4 = Calendar.getInstance(); 
//			calendar4.setTime(dNow);
//			calendar4.add(Calendar.DAY_OF_MONTH, 4); 
//			dValidEnd = calendar4.getTime(); 
			
            bean.setAcquireStartTime(dAcqBefore);
            bean.setAcquireEndTime(dAcqAfter);
			bean.setEffectiveStartTime(null);
			bean.setEffectiveEndTime(null);
			bean.setPlatformActivityId(sellercreatebatchBean.getCouponTypeBizValue());
			bean.setOperator(Integer.toString(new TestDataManager().getRandomIntNum(5)));
			call.setData(bean);
			call.callService();	
			//验证数据库
			VerifyReturnDataBase();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_changetimeinactivity_003() {
		Logger.start(true,"正例-改变优惠券批次时间，验证更新过期的开始结束时间，返回成功");
		try {
			Logger.comment("#######前置条件开始#########");
			sellercreatebatchBean = new CouponServiceMCallServiceV2().setDefaultSellerCreateBatch_ActivityBean();
			sellercreatebatchCall=new CouponServiceMCallServiceV2().SellerCreateBatch(sellercreatebatchBean);
			Logger.comment("#######前置条件结束#########");
			
		   beforBatch= new couponbatchWapper().selectBybatchCode(sellercreatebatchCall.getBatchCode()).get(0);
			
			Date dNow = new Date(); // 当前时间
			Date dAcqBefore = new Date();
			Date dAcqAfter = new Date();
//			Date dValidStar = new Date();
//			Date dValidEnd = new Date();
			
			Calendar calendar1 = Calendar.getInstance(); 
			calendar1.setTime(dNow);
			calendar1.add(Calendar.DAY_OF_MONTH, 1); 
			dAcqBefore = calendar1.getTime(); 

			Calendar calendar2 = Calendar.getInstance(); 
			calendar2.setTime(dNow);
			calendar2.add(Calendar.DAY_OF_MONTH, 2); 
			dAcqAfter = calendar2.getTime(); 
			
//			Calendar calendar3 = Calendar.getInstance(); 
//			calendar3.setTime(dNow);
//			calendar3.add(Calendar.DAY_OF_MONTH, 3); 
//			dValidStar = calendar3.getTime(); 
//			
//			Calendar calendar4 = Calendar.getInstance(); 
//			calendar4.setTime(dNow);
//			calendar4.add(Calendar.DAY_OF_MONTH, 4); 
//			dValidEnd = calendar4.getTime(); 
			
            bean.setAcquireStartTime(null);
            bean.setAcquireEndTime(null);
			bean.setEffectiveStartTime(dAcqBefore);
			bean.setEffectiveEndTime(dAcqAfter);
			bean.setPlatformActivityId(sellercreatebatchBean.getCouponTypeBizValue());
			bean.setOperator(Integer.toString(new TestDataManager().getRandomIntNum(5)));
			call.setData(bean);
			call.callService();	
			//验证数据库
			VerifyReturnDataBase();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_changetimeinactivity_004() {
		Logger.start(true,"正例-改变优惠券批次时间，验证更新使用开始时间，返回成功");
		try {
			Logger.comment("#######前置条件开始#########");
			sellercreatebatchBean = new CouponServiceMCallServiceV2().setDefaultSellerCreateBatch_ActivityBean();
			sellercreatebatchCall=new CouponServiceMCallServiceV2().SellerCreateBatch(sellercreatebatchBean);
			Logger.comment("#######前置条件结束#########");
			
		   beforBatch= new couponbatchWapper().selectBybatchCode(sellercreatebatchCall.getBatchCode()).get(0);
			
			Date dNow = new Date(); // 当前时间
			Date dAcqBefore = new Date();
//			Date dAcqAfter = new Date();
//			Date dValidStar = new Date();
//			Date dValidEnd = new Date();
			
			Calendar calendar1 = Calendar.getInstance(); 
			calendar1.setTime(dNow);
			calendar1.add(Calendar.DAY_OF_MONTH, 1); 
			dAcqBefore = calendar1.getTime(); 

//			Calendar calendar2 = Calendar.getInstance(); 
//			calendar2.setTime(dNow);
//			calendar2.add(Calendar.DAY_OF_MONTH, 2); 
//			dAcqAfter = calendar2.getTime(); 
			
//			Calendar calendar3 = Calendar.getInstance(); 
//			calendar3.setTime(dNow);
//			calendar3.add(Calendar.DAY_OF_MONTH, 3); 
//			dValidStar = calendar3.getTime(); 
//			
//			Calendar calendar4 = Calendar.getInstance(); 
//			calendar4.setTime(dNow);
//			calendar4.add(Calendar.DAY_OF_MONTH, 4); 
//			dValidEnd = calendar4.getTime(); 
			
            bean.setAcquireStartTime(dAcqBefore);
            bean.setAcquireEndTime(null);
			bean.setEffectiveStartTime(null);
			bean.setEffectiveEndTime(null);
			bean.setPlatformActivityId(sellercreatebatchBean.getCouponTypeBizValue());
			bean.setOperator(Integer.toString(new TestDataManager().getRandomIntNum(5)));
			call.setData(bean);
			call.callService();	
			//验证数据库
			VerifyReturnDataBase();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_changetimeinactivity_005() {
		Logger.start(true,"正例-改变优惠券批次时间，验证更新过期的开始时间，返回成功");
		try {
			Logger.comment("#######前置条件开始#########");
			sellercreatebatchBean = new CouponServiceMCallServiceV2().setDefaultSellerCreateBatch_ActivityBean();
			sellercreatebatchCall=new CouponServiceMCallServiceV2().SellerCreateBatch(sellercreatebatchBean);
			Logger.comment("#######前置条件结束#########");
			
		   beforBatch= new couponbatchWapper().selectBybatchCode(sellercreatebatchCall.getBatchCode()).get(0);
			
			Date dNow = new Date(); // 当前时间
//			Date dAcqBefore = new Date();
//			Date dAcqAfter = new Date();
			Date dValidStar = new Date();
//			Date dValidEnd = new Date();
			
//			Calendar calendar1 = Calendar.getInstance(); 
//			calendar1.setTime(dNow);
//			calendar1.add(Calendar.DAY_OF_MONTH, 1); 
//			dAcqBefore = calendar1.getTime(); 

//			Calendar calendar2 = Calendar.getInstance(); 
//			calendar2.setTime(dNow);
//			calendar2.add(Calendar.DAY_OF_MONTH, 2); 
//			dAcqAfter = calendar2.getTime(); 
			
			Calendar calendar3 = Calendar.getInstance(); 
			calendar3.setTime(dNow);
			calendar3.add(Calendar.DAY_OF_MONTH, 3); 
			dValidStar = calendar3.getTime(); 
//			
//			Calendar calendar4 = Calendar.getInstance(); 
//			calendar4.setTime(dNow);
//			calendar4.add(Calendar.DAY_OF_MONTH, 4); 
//			dValidEnd = calendar4.getTime(); 
			
            bean.setAcquireStartTime(null);
            bean.setAcquireEndTime(null);
			bean.setEffectiveStartTime(dValidStar);
			bean.setEffectiveEndTime(null);
			bean.setPlatformActivityId(sellercreatebatchBean.getCouponTypeBizValue());
			bean.setOperator(Integer.toString(new TestDataManager().getRandomIntNum(5)));
			call.setData(bean);
			call.callService();	
			//验证数据库
			VerifyReturnDataBase();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_changetimeinactivity_006() {
		Logger.start(false,"正例-改变优惠券批次时间，验证活动Id PlatformActivityId为空，返回成功");
		try {
			Logger.comment("#######前置条件开始#########");
			sellercreatebatchBean = new CouponServiceMCallServiceV2().setDefaultSellerCreateBatch_ActivityBean();
			sellercreatebatchCall=new CouponServiceMCallServiceV2().SellerCreateBatch(sellercreatebatchBean);
			Logger.comment("#######前置条件结束#########");
			
			Date dNow = new Date(); // 当前时间
			Date dAcqBefore = new Date();
			Date dAcqAfter = new Date();
			Date dValidStar = new Date();
			Date dValidEnd = new Date();
			
			Calendar calendar1 = Calendar.getInstance(); 
			calendar1.setTime(dNow);
			calendar1.add(Calendar.DAY_OF_MONTH, 1); 
			dAcqBefore = calendar1.getTime(); 

			Calendar calendar2 = Calendar.getInstance(); 
			calendar2.setTime(dNow);
			calendar2.add(Calendar.DAY_OF_MONTH, 2); 
			dAcqAfter = calendar2.getTime(); 
			
			Calendar calendar3 = Calendar.getInstance(); 
			calendar3.setTime(dNow);
			calendar3.add(Calendar.DAY_OF_MONTH, 3); 
			dValidStar = calendar3.getTime(); 
			
			Calendar calendar4 = Calendar.getInstance(); 
			calendar4.setTime(dNow);
			calendar4.add(Calendar.DAY_OF_MONTH, 4); 
			dValidEnd = calendar4.getTime(); 
			
            bean.setAcquireStartTime(dAcqBefore);
            bean.setAcquireEndTime(dAcqAfter);
			bean.setEffectiveStartTime(dValidStar);
			bean.setEffectiveEndTime(dValidEnd);
		//	bean.setPlatformActivityId(sellercreatebatchBean.getCouponTypeBizValue());
			bean.setOperator(Integer.toString(new TestDataManager().getRandomIntNum(5)));
			call.setData(bean);
			call.callService();	
			//验证数据库
			 Logger.verifyEquals(true,call.getMessage().contains("platformActivityId must"), "验证platformActivityId");
			 Logger.verifyEquals("1",call.getErrorCode(), "验证ErrorCode");
			 Logger.verifyEquals(0,call.getTaskId(), "验证ErrorCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_changetimeinactivity_007() {
		Logger.start(false,"正例-改变优惠券批次时间，验证 Operator为空，返回成功");
		try {
			Logger.comment("#######前置条件开始#########");
			sellercreatebatchBean = new CouponServiceMCallServiceV2().setDefaultSellerCreateBatch_ActivityBean();
			sellercreatebatchCall=new CouponServiceMCallServiceV2().SellerCreateBatch(sellercreatebatchBean);
			Logger.comment("#######前置条件结束#########");
			
			Date dNow = new Date(); // 当前时间
			Date dAcqBefore = new Date();
			Date dAcqAfter = new Date();
			Date dValidStar = new Date();
			Date dValidEnd = new Date();
			
			Calendar calendar1 = Calendar.getInstance(); 
			calendar1.setTime(dNow);
			calendar1.add(Calendar.DAY_OF_MONTH, 1); 
			dAcqBefore = calendar1.getTime(); 

			Calendar calendar2 = Calendar.getInstance(); 
			calendar2.setTime(dNow);
			calendar2.add(Calendar.DAY_OF_MONTH, 2); 
			dAcqAfter = calendar2.getTime(); 
			
			Calendar calendar3 = Calendar.getInstance(); 
			calendar3.setTime(dNow);
			calendar3.add(Calendar.DAY_OF_MONTH, 3); 
			dValidStar = calendar3.getTime(); 
			
			Calendar calendar4 = Calendar.getInstance(); 
			calendar4.setTime(dNow);
			calendar4.add(Calendar.DAY_OF_MONTH, 4); 
			dValidEnd = calendar4.getTime(); 
			
            bean.setAcquireStartTime(dAcqBefore);
            bean.setAcquireEndTime(dAcqAfter);
			bean.setEffectiveStartTime(dValidStar);
			bean.setEffectiveEndTime(dValidEnd);
			bean.setPlatformActivityId(sellercreatebatchBean.getCouponTypeBizValue());
			//bean.setOperator(Integer.toString(new TestDataManager().getRandomIntNum(5)));
			call.setData(bean);
			call.callService();	
			//验证数据库
			 Logger.verifyEquals(true,call.getMessage().contains("operator must"), "验证platformActivityId");
			 Logger.verifyEquals("1",call.getErrorCode(), "验证ErrorCode");
			 Logger.verifyEquals(0,call.getTaskId(), "验证ErrorCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	public void VerifyReturnDataBase() throws InterruptedException 
	{
		Thread.sleep(10000);
		//couponbatch
		checkcouponBatch();
	    //coupontask
		checkcouponTask();
	}
	
	 private void checkcouponBatch()
	 {
		 Logger.comment("#######验证数据库couponBatch#############");
		 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 couponbatch batch= new couponbatchWapper().selectBybatchCode(sellercreatebatchCall.getBatchCode()).get(0);
		 if(bean.getAcquireStartTime()!=null)
		    Logger.verifyEquals(formatter.format(bean.getAcquireStartTime()),formatter.format(batch.getAcquireStartTime()), "验证couponbatch表AcquireStartTime");
		 else
		    Logger.verifyEquals(formatter.format(beforBatch.getAcquireStartTime()),formatter.format(batch.getAcquireStartTime()), "验证couponbatch表AcquireStartTime");
		 
		 if(bean.getAcquireEndTime()!=null)
			Logger.verifyEquals(formatter.format(bean.getAcquireEndTime()), formatter.format(batch.getAcquireEndTime()), "验证couponbatch表AcquireEndTime");
		 else
			Logger.verifyEquals(formatter.format(beforBatch.getAcquireEndTime()),formatter.format(batch.getAcquireEndTime()), "验证couponbatch表AcquireEndTime");
		 
		 if(bean.getEffectiveStartTime()!=null)
			Logger.verifyEquals(formatter.format(bean.getEffectiveStartTime()), formatter.format(batch.getValidStart()), "验证couponbatch表ValidStart");
		 else
			Logger.verifyEquals(formatter.format(beforBatch.getValidStart()),formatter.format( batch.getValidStart()), "验证couponbatch表ValidStart");
		 
		 if(bean.getEffectiveEndTime()!=null)
			Logger.verifyEquals(formatter.format(bean.getEffectiveEndTime()), formatter.format(batch.getValidEnd()), "验证couponbatch表ValidEnd");
		 else
			Logger.verifyEquals(formatter.format(beforBatch.getValidEnd()), formatter.format(batch.getValidEnd()), "验证couponbatch表ValidEnd"); 
	 }
	 
	 private void checkcouponTask()
	 {
		 Logger.comment("#######验证数据库couponTask#############");
		 coupontask task= new coupontaskWapper().selectByPrimaryKey(call.getTaskId());
		 Logger.verifyEquals(bean.getPlatformActivityId(), task.getSubjectId(), "验证coupontask表SubjectId");
		 
		 Logger.verifyEquals(bean.getOperator(), task.getCreater(), "验证coupontask表Creater"); 
	 }
}